﻿using GalaSoft.MvvmLight;
using KINLO.UI.Main.View;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using GalaSoft.MvvmLight.Command;
using KINLO.Model;
using KINLO.Data.Reporitory;
using System.Windows;
using GalaSoft.MvvmLight.Messaging;
using System.Transactions;
using KINLO.UI.Main.Services;
using KINLO.Model.InfoModel;

namespace KINLO.UI.Main.VM
{
    public class SettingViewVM : ViewModelBase
    {
        SettingView View;
        ConfigReporitory DB1;
        MachineReporitory DB2 = new MachineReporitory();
        ConfigReporitory DB3 = new ConfigReporitory();
        string GuidNullValue = "00000000-0000-0000-0000-000000000000";
        public SettingViewVM(SettingView view)
        {
            View = view;
            Init();
        }

        void Init()
        {
            DB1 = new ConfigReporitory();
            ParaCfg = DB1.GetSettings();
            var machines = DB2.GetAllMachines();
            FrontScanner = machines.Where(s => s.MachineType == 1).FirstOrDefault();
            BackScanner = machines.Where(s => s.MachineType == 3).FirstOrDefault();
            InjectionMachine = machines.Where(s => s.MachineType == 6).FirstOrDefault();
            var m1 = machines.Where(s => s.MachineType == 2).OrderBy(s => s.Port).ToList();
            var m2 = machines.Where(s => s.MachineType == 4).OrderBy(s => s.Port).ToList();
            var m3 = machines.Where(s => s.MachineType == 5).OrderBy(s => s.Port).ToList();
            if (m1 != null)
                ElecBalance1 = new ObservableCollection<Machine>(m1);
            if (m2 != null)
                ElecBalance2 = new ObservableCollection<Machine>(m2);
            if (m3 != null)
                ElecBalance3 = new ObservableCollection<Machine>(m3);
            MesSettingInit();
        }

        public bool EnbleMesOnOff { get; set; }
        public string Mes_Url1 { get; set; }
        public string Mes_Url2 { get; set; }
        public string Mes_Url3 { get; set; }
        public string Mes_Url4 { get; set; }
        public string Mes_Url5 { get; set; }
        public string Mes_Url6 { get; set; }
        public string Mes_Url7 { get; set; }
        public string Mes_Url8 { get; set; }
        public string Mes_Url9 { get; set; }
        public string Mes_Url10 { get; set; }

        public string Mes_Url4Time { get; set; }
        public string Mes_Url8Time { get; set; }
        public string Mes_Url9Time { get; set; }
        public string Mes_Url10Time { get; set; }


        void MesSettingInit()
        {
            InfoDataModel.Tmp.Load();
            EnbleMesOnOff = InfoDataModel.Tmp.EnbleMesOnOff;
            Mes_Url1 = InfoDataModel.Tmp.Url1;
            Mes_Url2 = InfoDataModel.Tmp.Url2;
            Mes_Url3 = InfoDataModel.Tmp.Url3;
            Mes_Url4 = InfoDataModel.Tmp.Url4;
            Mes_Url5 = InfoDataModel.Tmp.Url5;
            Mes_Url6 = InfoDataModel.Tmp.Url6;
            Mes_Url7 = InfoDataModel.Tmp.Url7;
            Mes_Url8 = InfoDataModel.Tmp.Url8;
            Mes_Url9 = InfoDataModel.Tmp.Url9;
            Mes_Url10 = InfoDataModel.Tmp.Url10;

            Mes_Url4Time = InfoDataModel.Tmp.Url4Time;
            //InfoDataModel.Tmp.Url8Time = Mes_Url8Time;
            Mes_Url9Time = InfoDataModel.Tmp.Url9Time;
            Mes_Url10Time = InfoDataModel.Tmp.Url10Time;
        }

        void MesSettingSave()
        {
            InfoDataModel.Tmp.EnbleMesOnOff = EnbleMesOnOff;
            InfoDataModel.Tmp.Url1 = Mes_Url1;
            InfoDataModel.Tmp.Url2 = Mes_Url2;
            InfoDataModel.Tmp.Url3 = Mes_Url3;
            InfoDataModel.Tmp.Url4 = Mes_Url4;
            InfoDataModel.Tmp.Url5 = Mes_Url5;
            InfoDataModel.Tmp.Url6 = Mes_Url6;
            InfoDataModel.Tmp.Url7 = Mes_Url7;
            InfoDataModel.Tmp.Url8 = Mes_Url8;
            InfoDataModel.Tmp.Url9 = Mes_Url9;
            InfoDataModel.Tmp.Url10 = Mes_Url10;

            InfoDataModel.Tmp.Url4Time = Mes_Url4Time;
            //InfoDataModel.Tmp.Url8Time = Mes_Url8Time;
            InfoDataModel.Tmp.Url9Time = Mes_Url9Time;
            InfoDataModel.Tmp.Url10Time = Mes_Url10Time;

            InfoDataModel.Tmp.Save();
        }

        /// <summary>
        /// 前称重
        /// </summary>
        ObservableCollection<Machine> elecBalance1;
        public ObservableCollection<Machine> ElecBalance1
        {
            get { return elecBalance1; }
            set
            {
                elecBalance1 = value;
            }
        }

        /// <summary>
        /// 后称重
        /// </summary>
        ObservableCollection<Machine> elecBalance2;
        public ObservableCollection<Machine> ElecBalance2
        {
            get { return elecBalance2; }
            set
            {
                elecBalance2 = value;
            }
        }

        /// <summary>
        /// 补液称重
        /// </summary>
        ObservableCollection<Machine> elecBalance3;
        public ObservableCollection<Machine> ElecBalance3
        {
            get { return elecBalance3; }
            set
            {
                elecBalance3 = value;
            }
        }

        Machine frontScanner;
        public Machine FrontScanner
        {
            get { return frontScanner; }
            set
            {
                frontScanner = value;
                RaisePropertyChanged("FrontScanner");
            }
        }
        Machine backScanner;
        public Machine BackScanner
        {
            get { return backScanner; }
            set
            {
                backScanner = value;
                RaisePropertyChanged("BackScanner");
            }
        }
        Machine injectionMachine;
        public Machine InjectionMachine
        {
            get { return injectionMachine; }
            set
            {
                injectionMachine = value;
                RaisePropertyChanged("InjectionMachine");
            }
        }

        RelayCommand cmdExit;
        public RelayCommand CmdExit
        {
            get { return cmdExit ?? (cmdExit = new RelayCommand(() => DoCmdExit())); }

        }
        void DoCmdExit()
        {
            Messenger.Default.Unregister(this);
            View.Close();
        }

        RelayCommand cmdSave;
        public RelayCommand CmdSave
        {
            get { return cmdSave ?? (cmdSave = new RelayCommand(() => DoCmdSave())); }

        }
        void DoCmdSave()
        {
            //基本设置
            DB1.UpdateSettiings(ParaCfg);
            //前扫码
            if (!string.IsNullOrEmpty(FrontScanner.IP) && !string.IsNullOrEmpty(FrontScanner.Port.ToString()))
            {
                if (FrontScanner.ID.ToString() == GuidNullValue)
                {
                    FrontScanner.ID = Guid.NewGuid();
                    DB2.AddMachine(FrontScanner);
                }
                else
                {
                    DB2.UpdateMachine(FrontScanner);
                }
            }
            //后扫码
            if (!string.IsNullOrEmpty(BackScanner.IP) && !string.IsNullOrEmpty(BackScanner.Port.ToString()))
            {
                if (BackScanner.ID.ToString() == GuidNullValue)
                {
                    BackScanner.ID = Guid.NewGuid();
                    DB2.AddMachine(BackScanner);
                }
                else
                {
                    DB2.UpdateMachine(BackScanner);
                }
            }
            //注液机
            if (!string.IsNullOrEmpty(InjectionMachine.IP) && !string.IsNullOrEmpty(InjectionMachine.Port.ToString()))
            {
                if (InjectionMachine.ID.ToString() == GuidNullValue)
                {
                    InjectionMachine.ID = Guid.NewGuid();
                    DB2.AddMachine(InjectionMachine);
                }
                else
                {
                    DB2.UpdateMachine(InjectionMachine);
                }
            }
            var machines = DB2.GetAllMachines();
            //前称重
            if (ElecBalance1 != null)
            {
                DB2.DeleteMachineByMachineType(2);
                foreach (var el in ElecBalance1)
                {
                    if (!string.IsNullOrEmpty(el.IP) && !string.IsNullOrEmpty(el.Port.ToString()))
                    {
                        el.ID = Guid.NewGuid();
                        el.MachineType = 2;
                        el.Remark = "前称重";
                        DB2.AddMachine(el);
                    }
                }
            }
            //后称重
            if (ElecBalance2 != null)
            {
                DB2.DeleteMachineByMachineType(4);
                foreach (var el in ElecBalance2)
                {
                    if (!string.IsNullOrEmpty(el.IP) && !string.IsNullOrEmpty(el.Port.ToString()))
                    {
                        el.ID = Guid.NewGuid();
                        el.MachineType = 4;
                        el.Remark = "后称重";
                        DB2.AddMachine(el);
                    }
                }
            }
            //补液称重
            if (ElecBalance3 != null)
            {
                DB2.DeleteMachineByMachineType(5);
                foreach (var el in ElecBalance3)
                {
                    if (!string.IsNullOrEmpty(el.IP) && !string.IsNullOrEmpty(el.Port.ToString()))
                    {
                        el.ID = Guid.NewGuid();
                        el.MachineType = 5;
                        el.Remark = "补液称重";
                        DB2.AddMachine(el);
                    }
                }
            }
            GlobalValues.Init();
            MesSettingSave();
            if (MessageBoxResult.OK == MessageBox.Show("保存成功,配置在重启注液系统后才会生效", "提示", MessageBoxButton.OKCancel, MessageBoxImage.Information))
            {
                DoCmdExit();
            }
        }

        ParaConfig paraCfg;
        public ParaConfig ParaCfg
        {
            get { return paraCfg; }
            set
            {
                paraCfg = value;
                RaisePropertyChanged("ParaCfg");
            }
        }
    }
}
